const moment = require('moment');
module.exports = app => {
  const {STRING, DATE, BIGINT, INTEGER} = app.Sequelize;

  const Folder = app.model.define('folder', {
    id: {
      type: BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    name: STRING(100),
    create_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    modify_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('modify_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    create_username: STRING(100),
    create_userid: BIGINT,
    parent_id: BIGINT,
    grand_parent_id: BIGINT,
    delete_flag: INTEGER,
  }, {
    tableName: 'hy_folder',
    timestamps: false,
  });

  Folder.associate = function() {
    app.model.Folder.hasMany(app.model.Folder, {
      as: 'children',
      foreignKey: 'parent_id',
      targetKey: 'id'
    });
    app.model.Folder.hasMany(app.model.Resource, {
      as: 'files',
      foreignKey: 'folder_id',
      targetKey: 'id'
    });
    app.model.Folder.belongsTo(app.model.Folder, {
      as: 'parent',
      foreignKey: 'parent_id',
      targetKey: 'id'
    })
    app.model.Folder.belongsTo(app.model.Folder, {
      as: 'grandParent',
      foreignKey: 'grand_parent_id',
      targetKey: 'id'
    })
  };

  return Folder;
};
